package com.jboltai.capability.vdb.doc;

import com.jboltai.util.other.StringUtil;

import java.util.HashMap;
import java.util.List;

/**
 * 文档
 */
public class VDBDoc {


    private String id;

    /**
     * 文本
     */
    private String doc;

    /**
     * 查询得分
     */
    private Double score;

    /**
     * doc转化的向量
     */
    private List<Double> vector;

    /**
     * 其他字段值
     */
    private HashMap<String, Object> fieldValues;

    public VDBDoc() {
    }

    public VDBDoc(String id) {
        this.id = id;
    }

    public VDBDoc(String id, String doc) {
        this.id = id;
        this.doc = doc;
    }

    public VDBDoc(String id, String doc, HashMap<String, Object> fieldValues) {
        this.id = id;
        this.doc = doc;
        this.fieldValues = fieldValues;
    }

    /**
     * 设置字段和值
     * @param fieldName
     * @param value
     * @return
     */
    public VDBDoc setField(String fieldName, Object value) {
        if (this.fieldValues == null) {
            this.fieldValues = new HashMap<>();
        }
        this.fieldValues.put(fieldName, value);
        return this;
    }

    public <T> T getField(String fieldName) {
        if (this.fieldValues == null) {
            return null;
        }
        return (T) this.fieldValues.get(fieldName);
    }

    public <T> T getFieldAs(String fieldName, Class<T> type) {
        if (this.fieldValues == null) {
            return null;
        }
        return StringUtil.convert(this.fieldValues.get(fieldName).toString(), type);
    }


    /**
     * 移除某个字段的值
     * @param fieldName
     * @return
     */
    public VDBDoc removeField(String fieldName) {
        if (this.fieldValues != null) {
            this.fieldValues.remove(fieldName);
        }
        return this;
    }


    public String getId() {
        return id;
    }

    public VDBDoc setId(String id) {
        this.id = id;
        return this;
    }

    public String getDoc() {
        return doc;

    }

    public VDBDoc setDoc(String doc) {
        this.doc = doc;
        return this;
    }

    public HashMap<String, Object> getFieldValues() {
        return fieldValues;
    }

    public VDBDoc setFieldValues(HashMap<String, Object> fieldValues) {
        this.fieldValues = fieldValues;
        return this;

    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public List<Double> getVector() {
        return vector;
    }

    public void setVector(List<Double> vector) {
        this.vector = vector;
    }
}
